package com.meirengu.webview.controller;

import com.meirengu.common.StatusCode;
import com.meirengu.model.Result;
import com.meirengu.webview.service.KwOperationLogService;
import com.meirengu.webview.service.SmsService;
import com.meirengu.webview.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("handSms")
public class HandSmsController extends BaseController {


    @Autowired
    UserService userService;

    @Autowired
    SmsService smsService;

    @Autowired
    KwOperationLogService kwOperationLogService;

    /**
     * 手工发送黑卡短信
     *
     * @param createTimeBegin
     * @param createTimeEnd
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("/handBlackCardSms")
    public Result handXbb(@RequestParam(value = "createTimeBegin", required = true) String createTimeBegin,
                          @RequestParam(value = "createTimeEnd", required = true) String createTimeEnd) throws Exception {
        if (StringUtils.isBlank(createTimeBegin) || StringUtils.isBlank(createTimeEnd)) {
            return setResult(StatusCode.MISSING_ARGUMENT, null, StatusCode.codeMsgMap.get(StatusCode.MISSING_ARGUMENT));
        }
        //查出符合条件的用户
        Map<String, Object> map = new HashMap<>();
        if (StringUtils.isNotBlank(createTimeBegin)) {
            map.put("createTimeBegin", createTimeBegin);
        }
        if (StringUtils.isNotBlank(createTimeEnd)) {
            map.put("createTimeEnd", createTimeEnd);
        }
        List<Map<String, Object>> userList = userService.getList(map);
        kwOperationLogService.addOperationLog("2", " 发送短信", "handBlackCardSms",
                "手工发送黑卡短信", createTimeBegin, createTimeEnd, "kw_user", userList.size(),
                null + "", null, null);
        if (userList != null && userList.size() > 0) {
            for (Map<String, Object> userMap : userList) {
                if (userMap.get("mobile") != null) {
                    String mobile = userMap.get("mobile").toString();
                    if (StringUtils.isNotBlank(mobile)) {
                        //发送黑卡短信
                        String name = "老铁";
                        if (userMap.get("name") != null) {
                            if (StringUtils.isNotBlank(userMap.get("name").toString())) {
                                name = userMap.get("name").toString();
                            }
                        }
                        String text = "【环球黑卡】尊敬的" + name + ",恭喜您获得环球黑卡至尊卡特邀资格！高至20万！戳 http://t.cn/RmCokA9 取款激活码8退回T";
                        smsService.singleSendMarketing(mobile, text);
                    }
                }
            }
        }
        return super.setResult(StatusCode.OK, "", StatusCode.codeMsgMap.get(StatusCode.OK));
    }

}
